package com.example.demo.controller;

import com.example.demo.Service.UserFollowService;
import com.example.demo.entity.UserFollow;
import com.example.demo.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/follows")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    @PostMapping("/follow")
    public ResponseEntity<ApiResponse> followUser(@RequestParam Long followerId, @RequestParam Long followeeId) {
        userFollowService.followUser(followerId, followeeId);
        ApiResponse response = new ApiResponse(true, null, "User followed successfully.");
        return ResponseEntity.ok(response);
    }

    @PostMapping("/unfollow")
    public ResponseEntity<ApiResponse> unfollowUser(@RequestParam Long followerId, @RequestParam Long followeeId) {
        userFollowService.unfollowUser(followerId, followeeId);
        ApiResponse response = new ApiResponse(true, null, "User unfollowed successfully.");
        return ResponseEntity.ok(response);
    }

    @GetMapping("/isFollowed")
    public ResponseEntity<ApiResponse> isFollowed(@RequestParam Long followerId, @RequestParam Long followeeId) {
        boolean isFollowed = userFollowService.isFollowed(followerId, followeeId);
        ApiResponse response = new ApiResponse(isFollowed, isFollowed, isFollowed ? "User is followed." : "User is not followed.");
        return ResponseEntity.ok(response);
    }

    @GetMapping("/followers")
    public ResponseEntity<ApiResponse<List<UserFollow>>> getFollowers(@RequestParam Long followerId) {
        List<UserFollow> followersList = userFollowService.getFollowersList(followerId);
        ApiResponse<List<UserFollow>> response = new ApiResponse<>(true, followersList, "Followers list retrieved successfully.");
        return ResponseEntity.ok(response);
    }
}

